#ifndef HUFFMAN_CODER_H
#define HUFFMAN_CODER_H

#include "predictor_header.h"
#include "file_saver.h"
#include "file_reader.h"
#include <QMap>
#include <QString>
#include <QMultiMap>
#include <QList>
#include <cmath>


class node
{
public:
    node();
    //~node();
    node *left, *right;
    int value;
    long long int weight;
    void delete_tree(node *root);
};

struct code
{
    int value;
    short bits;
};

class node_list
{
public:
    node *element;
    node *minimum(node *no);
    node_list *next;
    void delete_element(node *del);
};

class huffman_coder
{
public:
    huffman_coder();
    ~huffman_coder();
    void compress(predictor_header *predictor, short *raw_data, QString file);
    short* decompress(predictor_header *predictor, QString file);

private:
    predictor_header *parameters;
    short *data, *dedata;
    QString path;
    void count_statistic();
    node *root;
    int statistic[255*2+1];
    void create_tree();
    void make_codes(node *root, int value, short bits);
    QMap <short, code> key;
    QMultiMap <int, short> dekey;
    QMultiMap <int, short> bits;
    void compress_to_file();
    unsigned short char_count;
    void print_tree (node* root, int value, short bits);
    void decompress_from_file();
    void make_decodes(node *root, int value, short bites);
};

#endif // HUFFMAN_CODER_H
